﻿// Accord Statistics Library
// The Accord.NET Framework
// http://accord-framework.net
//
// Copyright © César Souza, 2009-2017
// cesarsouza at gmail.com
//
//    This library is free software; you can redistribute it and/or
//    modify it under the terms of the GNU Lesser General Public
//    License as published by the Free Software Foundation; either
//    version 2.1 of the License, or (at your option) any later version.
//
//    This library is distributed in the hope that it will be useful,
//    but WITHOUT ANY WARRANTY; without even the implied warranty of
//    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
//    Lesser General Public License for more details.
//
//    You should have received a copy of the GNU Lesser General Public
//    License along with this library; if not, write to the Free Software
//    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
//

namespace Accord.MachineLearning
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using Accord.Math;
    using Accord.Compat;
    using System.Threading.Tasks;

    /// <summary>
    ///   Base class for multi-class and multi-label classifiers.
    /// </summary>
    /// 
    /// <typeparam name="TInput">The data type for the input data. Default is double[].</typeparam>
    /// 
    [Serializable]
    public abstract class TaggerBase<TInput> :
        TransformBase<TInput[], int[]>,
        ITagger<TInput>
    {

        /// <summary>
        /// Gets the number of classes expected and recognized by the classifier.
        /// </summary>
        /// <value>The number of classes.</value>
        public virtual int NumberOfClasses
        {
            get { return NumberOfOutputs; }
            set { NumberOfOutputs = value; }
        }

        /// <summary>
        /// Computes class-label decisions for the given <paramref name="input" />.
        /// </summary>
        /// <param name="input">The input vectors that should be classified as
        /// any of the <see cref="ITransform.NumberOfOutputs" /> possible classes.</param>
        /// <param name="result">The location where to store the class-labels.</param>
        /// <returns>
        /// A set of class-labels that best describe the <paramref name="input" />
        /// vectors according to this classifier.
        /// </returns>
        public virtual int[] Decide(TInput[] input, int[] result)
        {
            return Decide(new[] { input }, new[] { result })[0];
        }

        /// <summary>
        /// Computes class-label decisions for the given <paramref name="input" />.
        /// </summary>
        /// <param name="input">The input vectors that should be classified as
        /// any of the <see cref="ITransform.NumberOfOutputs" /> possible classes.</param>
        /// <returns>
        /// A set of class-labels that best describe the <paramref name="input" />
        /// vectors according to this classifier.
        /// </returns>
        public int[] Decide(TInput[] input)
        {
            return Decide(input, new int[input.Length]);
        }

        /// <summary>
        /// Computes class-label decisions for the given <paramref name="input" />.
        /// </summary>
        /// <param name="input">The input vectors that should be classified as
        /// any of the <see cref="ITransform.NumberOfOutputs" /> possible classes.</param>
        /// <returns>
        /// A set of class-labels that best describe the <paramref name="input" />
        /// vectors according to this classifier.
        /// </returns>
        public int[][] Decide(TInput[][] input)
        {
            return Decide(input, Jagged.CreateAs<TInput, int>(input));
        }

        /// <summary>
        /// Computes class-label decisions for the given <paramref name="input" />.
        /// </summary>
        /// <param name="input">The input vectors that should be classified as
        /// any of the <see cref="ITransform.NumberOfOutputs" /> possible classes.</param>
        /// <param name="result">The location where to store the class-labels.</param>
        /// <returns>
        /// A set of class-labels that best describe the <paramref name="input" />
        /// vectors according to this classifier.
        /// </returns>
        public abstract int[][] Decide(TInput[][] input, int[][] result);

        /// <summary>
        /// Applies the transformation to an input, producing an associated output.
        /// </summary>
        /// <param name="input">The input data to which the transformation should be applied.</param>
        /// <returns>
        /// The output generated by applying this transformation to the given input.
        /// </returns>
        public override int[] Transform(TInput[] input)
        {
            return Decide(input);
        }


        internal double[][] create(TInput[] input)
        {
            var result = new double[input.Length][];
            for (int j = 0; j < input.Length; j++)
                result[j] = new double[NumberOfOutputs];
            return result;
        }

        internal double[][][] create(TInput[][] input)
        {
            var result = new double[input.Length][][];
            for (int i = 0; i < input.Length; i++)
            {
                result[i] = new double[input[i].Length][];
                for (int j = 0; j < input[i].Length; j++)
                    result[i][j] = new double[NumberOfOutputs];
            }

            return result;
        }

        internal T[][] create<T>(TInput[][] input, T[][] decision)
        {
            if (decision == null)
                decision = Jagged.CreateAs<TInput, T>(input);
            return decision;
        }


    }
}
